// pages/member/favorite/favorite.js
const app = getApp()
import {
        list, del
} from '../../../api/favorite'

// const util = require('../../utils/util.js')

Page({

        /**
         * 页面的初始数据
         */
        data: {
                        havefavorite: false,
                        pageName: 1,
                        pageSize:20,
                        favoriteList: [
                        // {
                        //         id: 1,
                        //         state: false,
                        //         tag: "下架",
                        //         thumbnail: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
                        //         name: "【SK-II】CPB雪域 洗面奶水乳套装",
                        //         price: 180.00,
                        //         subTitle: "我是最棒的，没有之一"
                        // },
                        // {
                        //         id: 1,
                        //         state: false,
                        //         tag: "下架",
                        //         thumbnail: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
                        //         name: "【SK-II】CPB雪域 洗面奶水乳套装",
                        //         price: 180.00,
                        //         subTitle: "我是最棒的，没有之一"
                        // },
                        // {
                        //         id: 1,
                        //         state: false,
                        //         tag: "下架",
                        //         thumbnail: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
                        //         name: "【SK-II】CPB雪域 洗面奶水乳套装",
                        //         price: 180.00,
                        //         subTitle: "我是最棒的，没有之一"
                        // }
                ]
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function(options) {
                
        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function() {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function() {
                this.getFavoriteData((data) => {
                        let havefavorite = ""
                        if (data.length > 0){
                                havefavorite = true
                        }  
                                this.setData({
                                        favoriteList: data,
                                        havefavorite: havefavorite
                                })
                })
        },

        //获取收藏夹的数据
        getFavoriteData(callback) {
                list().then(
                        res => {
                                console.log("接收收藏夹信息--")
                                console.log(res)
                                if (res.type == 'success') {
                                        if (callback){
                                                callback(res.data.data)
                                        }
                                }
                        }
                ).catch(error => {
                        console.log(error)
                })
        },

        //删除收藏内容
        deleteItem(e){
                console.log(e.currentTarget.dataset.id)
                let _this = this
                wx.showModal({
                        title: '温馨提示',
                        content: '确定要取消收藏该商品',
                        success(res) {
                                if (res.confirm) {
                                        del(e.currentTarget.dataset.id).then(
                                                res => {
                                                        console.log("接收收藏夹信息--" + JSON.stringify(res))
                                                        if (res.type == 'success') {
                                                                let favoriteList = _this.data.favoriteList
                                                                if (favoriteList.length > 0){
                                                                        let delIndex = ""
                                                                        favoriteList.map((item,index)=>{
                                                                                if (item.id == e.currentTarget.dataset.id){
                                                                                        delIndex = index        
                                                                                }
                                                                        })
                                                                        favoriteList.splice(delIndex, 1) 
                                                                        let havefavorite
                                                                        if (favoriteList.length == 0){
                                                                                havefavorite = false
                                                                        }
                                                                        _this.setData({
                                                                                favoriteList: favoriteList,
                                                                                havefavorite: havefavorite
                                                                        })
                                                                        wx.showToast({
                                                                                title: '取消成功',
                                                                                icon:'none',
                                                                                duration:1500
                                                                        })
                                                                } 
                                                        }
                                                }
                                        ).catch(error => {
                                                console.log(error)
                                        })
                                } 
                        }
                })
        },

        //去逛逛的事件
        goShopping(){
                wx.switchTab({
                        url: '/pages/index/index',
                        success: function(res) {},
                        fail: function(res) {},
                        complete: function(res) {},
                })
        },

        //监听页面的滚动事件
        onPageScroll: function (e) {
                console.log(e.scrollTop);
                this.data.scrollTop = e.scrollTop
        },

        //上啦加载
        onReachBottom(e) {
                let favoriteList = this.data.favoriteList
                this.getFavoriteData((data) => {
                        if (data.length = 0){
                                wx.showToast({
                                        title: '没有更多收藏了',
                                        icon:"none",
                                        duration:2000
                                })
                        }else{
                                favoriteList = favoriteList.concat(data)
                                this.setData({
                                        favoriteList: favoriteList
                                })
                        } 
                        let scrollTop = this.data.scrollTop - 30
                        wx.pageScrollTo({
                                scrollTop: scrollTop,
                                duration: 300
                        })
                })
        },
})